package com.tanhua.admin.module.dynamicmanage.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.admin.module.dynamicmanage.pojo.Totals;
import com.tanhua.admin.module.dynamicmanage.service.MessagesService;
import com.tanhua.admin.module.dynamicmanage.vo.CommentPageResult;
import com.tanhua.admin.module.dynamicmanage.vo.CommentVo;
import com.tanhua.admin.module.dynamicmanage.vo.MessagesVo;
import com.tanhua.admin.module.dynamicmanage.vo.PageResult;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.PublishInfo;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.api.CommentApi;
import com.tanhua.dubbo.server.api.MessagesApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.pojo.Publish;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 消息服务impl
 *
 * @author 忘川
 * @date 2021/04/21
 */
@Service
@Slf4j
public class MessagesServiceImpl implements MessagesService {


    @Autowired
    private PublishInfoServiceImpl publishInfoService;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference(version = "1.0.0")
    private MessagesApi messagesApi;

    @Reference(version = "1.0.0")
    private CommentApi commentApi;


    /**
     * 查询信息
     *
     * @param param 参数
     * @return {@link PageResult}
     */
    @Override
    public PageResult queryMessages(Map<String, Object> param) {

        //先查询各个表,将信息先查询出来,再进行分页

        //查询排序是在查表的时候构造sql语句的时候进行查询的

        //得到相应的参数
        Integer pagesize = Integer.valueOf(param.get("pagesize").toString());
        Integer page = Integer.valueOf(param.get("page").toString());
        Object sortProp = param.get("sortProp");
        Object sortOrder = param.get("sortOrder");
        Object userId = param.get("id");
        Object sd = param.get("sd");
        Object ed = param.get("ed");
        Object uid = param.get("uid");
        Object state = param.get("state");


        Map<String, Object> publishInfoMap = publishInfoService.queryPublishInfoList(page, pagesize, sortProp, sortOrder, userId,
                sd, ed, uid, state);
        //得到分页对象
        IPage<PublishInfo> publishInfoIPage=(IPage<PublishInfo>)publishInfoMap.get("publishInfoIPage");

        //得到圈子信息集合
        List<PublishInfo> publishInfoList = publishInfoIPage.getRecords();
        //用户信息
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*");

        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
        if (ObjectUtil.isEmpty(userInfoList)) {
            return null;
        }

        //查询发布表
        List<Publish> publishList = messagesApi.queryPublish();
        if (ObjectUtil.isEmpty(publishList)) {
            return null;
        }
        ArrayList<MessagesVo> messagesVos = new ArrayList<>();

        //先进行填充每条动态信息
        //将动态内容添加进动态
        ArrayList<String> medias = new ArrayList<>();
        medias.add("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/photo/6/1.jpg");
        medias.add("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/photo/6/CL-3.jpg");
        medias.add("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/photo/5/1564567252410.jpg");
        for (PublishInfo publishInfo : publishInfoList) {
            MessagesVo messagesVo = new MessagesVo();

            messagesVo.setUserId(publishInfo.getUserId());
            messagesVo.setState(publishInfo.getState());
            messagesVo.setId(publishInfo.getPublishId());
            messagesVo.setLikeCount(publishInfo.getLikeCount());
            messagesVo.setCommentCount(publishInfo.getCommentCount());
            messagesVo.setForwardingCount(publishInfo.getForwardingCount());

            messagesVo.setText("尽己力，听天命。无愧于心，不惑于情。顺势而为，随遇而安。知错就改，迷途知返。" +
                    "在喜欢自己的人身上用心，在不喜欢自己的人身上健忘。如此一生，甚好。");
            messagesVo.setMedias(medias.toArray(new String[]{}));
            messagesVo.setCreateDate(Long.parseLong("1605260225544"));

            messagesVos.add(messagesVo);

        }
        //用户信息填充
        for (MessagesVo messagesVo : messagesVos) {

            for (UserInfo userInfo : userInfoList) {
                //将用户信息补充完整
                if (messagesVo.getUserId() == userInfo.getUserId().longValue()) {
                    //messagesVo.setUserId(userInfo.getUserId().intValue());
                    messagesVo.setNickname(userInfo.getNickName());
                    messagesVo.setUserLogo(userInfo.getLogo());
                }
            }
        }


        //消息视图填充,将动态内容进行填充
       /* for (MessagesVo messagesVo : messagesVos) {


            for (Publish publish : publishList) {
                if (messagesVo.getId().equals(publish.getId().toHexString())) {
                    messagesVo.setText(publish.getText());
                    messagesVo.setMedias(publish.getMedias());
                    messagesVo.setCreateDate(publish.getCreated());
                }
            }

        }*/


        //填充返回数据类
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);

        pageResult.setItems(messagesVos);

        //查询相应状态的动态总数量
        Integer count = (Integer) publishInfoMap.get("count");
        pageResult.setCounts(count);

        //查询状态列表
        List<Totals> totalList = publishInfoService.queryStateList(userId,sd,ed);

        //将全部total的集合放入结果集
        pageResult.setTotals(totalList);


        return pageResult;//查询用户信息表,得到所有的用户信息

    }


    /**
     * 查询评论
     *
     * @param param 参数
     * @return {@link CommentPageResult}
     */
    @Override
    public CommentPageResult queryComment(Map<String, Object> param)  {

        Integer pagesize = Integer.valueOf(param.get("pagesize").toString());
        Integer page = Integer.valueOf(param.get("page").toString());
        String sortProp = param.get("sortProp").toString();
        String sortOrder = param.get("sortOrder").toString();
        String messageID = param.get("messageID").toString();

        //创建返回结果集
        CommentPageResult commentPageResult = new CommentPageResult();

        //获取评论列表
        Map<String, Object> map = commentApi.queryCommentList(page, pagesize, sortProp, sortOrder, 2, messageID);

        List<Comment> commentList =(List<Comment>)map.get("commentList");

        ArrayList<Long> userIds = new ArrayList<>();
        //查询评论人的Id
        for (Comment comment : commentList) {
            userIds.add(comment.getUserId());
        }
        //查询用户列表
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIds);
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
        if (ObjectUtil.isEmpty(userInfoList)) {
            return null;
        }

        //创建评论视图列表
        ArrayList<CommentVo> commentVos = new ArrayList<>();
        //将评论信息填充到评论视图
        for (Comment comment : commentList) {
            CommentVo commentVo = new CommentVo();
            //填充圈子id
            commentVo.setId(comment.getId().toString());
            //填充评论内容
            commentVo.setContent(comment.getContent());
            //填充评论人id
            commentVo.setUserId(comment.getUserId());
            //填充评论时间
            commentVo.setCreateDate(comment.getCreated());
            commentVos.add(commentVo);
        }

        //填充评论人的信息

        for (CommentVo commentVo : commentVos) {
            for (UserInfo userInfo : userInfoList) {
                if (commentVo.getUserId().equals(userInfo.getUserId())) {
                    //填充用户昵称
                    commentVo.setNickname(userInfo.getNickName());
                }
            }

        }



        //设置评论总条数
        commentPageResult.setCounts(map.get("count").hashCode());
        //设置当前页
        commentPageResult.setPage(page);
        //设置当前页总数
        commentPageResult.setPage(pagesize);
        //设置评论数据
        commentPageResult.setItems(commentVos);

        return commentPageResult;
    }

    /**
     * 撤销操作,回到未审核状态
     * @param id
     * @return {@link Map<String, String>}
     */
    @Override
    public Map<String,String> undoAudit(String[] id) {

        Map<String, String> result = publishInfoService.undoAudit(id);
        return result;
    }


}
